<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . "/../database/$database/database.inc.php";
  
header('Content-Type: text/plain');

global $myManager;

$action = MiscUtils::getParam('action', NULL);

$myManager->start_t();
try {
  switch ($action) {
    case 'login':
      login($myManager);
      break;
    case 'smry':
      smry($myManager);
      break;
    case 'smrydeadline':
      smrydeadline($myManager);
      break;
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}
$myManager->commit_t();

function login($myManager) {
  $orm = classToOrm('user');
  $account = MiscUtils::getParam('a', NULL);
  $password = MiscUtils::getParam('p', START);

  $result = $orm->find($myManager, NULL, NULL, NULL, NULL, "d.account = '$account' AND d.password = '$password'", NULL);
  if (count($result->data) == 1) {
    $user = $result->data[0];
    $_SESSION[OPERATOR] = $user;
    
    setcookie(OPERATOR, json_encode($user), (time() + 3600), PATH, DOMAIN);
    
    echo json_encode($user);
  } else {
    echo json_encode(array());
  }
}

function smry($myManager) {
  $args = MiscUtils::getParam('a', NULL);
  $args = ($args) ? json_decode($args) : array();
  $branch = json_decode(MiscUtils::getParam('b', NULL));
  
  if ($branch) {
   $br = " AND d.branch = '$branch'";
  } else {
    $br = '';
  }
  
  $now = SimpleDate::fromStamp(time() + LAG);
  $now->hours = 0;
  $now->minutes = 0;
  $now->seconds = 0;
  $date = SimpleDate::toStamp($now);

  $smry = array();
  $orm = classToOrm('produce');
  foreach ($args as &$arg) {
    try {
      $smry[$arg->name] = array();
      $result = $orm->find($myManager, NULL, NULL, NULL, NULL, $arg->cond . $br, 'COUNT(d.oid)');
      $smry[$arg->name]['total'] = $result->data;
      $result = $orm->find($myManager, NULL, NULL, NULL, NULL, $arg->cond . $br . ' AND (d.deadline < ' . $date . ')', 'COUNT(d.oid)');
      $smry[$arg->name]['urgent'] = $result->data;
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
  echo json_encode($smry);
}

function smrydeadline($myManager) {
  $args = MiscUtils::getParam('a', NULL);
  $args = ($args) ? json_decode($args) : array();
  $branch = json_decode(MiscUtils::getParam('b', NULL));
  
  if ($branch) {
   $br = " AND d.branch = '$branch'";
  } else {
    $br = '';
  }
  
  $now = SimpleDate::fromStamp(time() + LAG);
  $now->hours = 0;
  $now->minutes = 0;
  $now->seconds = 0;
  $deadline = (SimpleDate::toStamp($now) - 259200);
  
  $smry = array();
  $orm = classToOrm('produce');
 
  foreach ($args as &$arg) {
    try {
      $smry[$arg->name] = array();
      $result = $orm->find($myManager, NULL, NULL, NULL, NULL, $arg->cond . $br . ' AND (d.deadline < ' . $deadline . ')', 'COUNT(d.oid)');
      $smry[$arg->name]['total'] = $result->data;
      $smry[$arg->name]['urgent'] = $result->data;
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
  echo json_encode($smry);
}

?>